//
//  MainFunctionViewController.m
//  ProfilePicture
//
//  Created by 张云江 on 2019/10/14.
//  Copyright © 2019年 张云江. All rights reserved.
//

#import "MainFunctionViewController.h"
#import "ExportViewController.h"
#import "ChangeBgView.h"
#import "LikeBeautyView.h"
#import "DressUpView.h"

@interface MainFunctionViewController ()<UIGestureRecognizerDelegate>

@property(nonatomic,strong)UIImageView      *photoImage;
@property(nonatomic,strong)NSMutableArray   *btnArr;

@property(nonatomic,strong)ChangeBgView     *changeBg;
@property(nonatomic,strong)LikeBeautyView   *likeBeauty;
@property(nonatomic,strong)DressUpView      *dressUp;
@property(nonatomic,strong)NSMutableArray   *viewArr;

@end

@implementation MainFunctionViewController

+ (MainFunctionViewController *)initWtihCutCheckPicInfo:(CutCheckPicInfo *)info
{
    MainFunctionViewController *vc = [[MainFunctionViewController alloc]init];
    vc.info = info;
    return vc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    WCDelegate.currbgCount = 0;
    
    [self.photoImage sd_setImageWithURL:[NSURL URLWithString:self.info.img_wm_url_list[WCDelegate.currbgCount]]];
    
}

- (void)setNav
{
    self.customNavBar.hidden = YES;
}

- (void)creatUI
{
    self.view.backgroundColor = kUIColorFromRGB(0xF3F5FF);
    
    self.btnArr = [NSMutableArray array];
    self.viewArr = [NSMutableArray array];
    
    self.photoImage = [[UIImageView alloc] init];
    self.photoImage.backgroundColor = kUIColorFromRGB(0xF3F5FF);
    [self.view addSubview:self.photoImage];
    [self.photoImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(kRatio(43));
        make.right.equalTo(self.view).offset(kRatio(-43));
        make.centerY.equalTo(self.view).offset(kRatio(-226/2));
        make.height.mas_equalTo(kRatio(386));
    }];
    
    UIView *footBg = [[UIView alloc]init];
    footBg.backgroundColor = [UIColor whiteColor];
    footBg.layer.cornerRadius = kRatio(26);
    footBg.layer.masksToBounds = YES;
    [self.view addSubview:footBg];
    [footBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self.view);
        make.height.mas_equalTo(kRatio(226));
    }];
    
    UIView *bottom = [[UIView alloc]init];
    [footBg addSubview:bottom];
    [bottom mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(footBg);
        make.height.mas_equalTo(kRatio(50));
    }];
    
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = kUIColorFromRGA(0x000000, 0.23);
    [bottom addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(bottom);
        make.height.mas_equalTo(kRatio(1));
    }];
    
    UIButton *mf_back = [[UIButton alloc]init];
    [mf_back setImage:[UIImage imageNamed:@"mf_back"] forState:UIControlStateNormal];
    [mf_back addTarget:self action:@selector(onmfBackClick) forControlEvents:UIControlEventTouchUpInside];
    [bottom addSubview:mf_back];
    [mf_back mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bottom).offset(kRatio(23));
        make.centerY.equalTo(bottom);
    }];
    
    UIButton *mf_forward = [[UIButton alloc]init];
    [mf_forward setImage:[UIImage imageNamed:@"mf_forward"] forState:UIControlStateNormal];
    [mf_forward addTarget:self action:@selector(onmfmForwardClick) forControlEvents:UIControlEventTouchUpInside];
    [bottom addSubview:mf_forward];
    [mf_forward mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bottom).offset(kRatio(-23));
        make.centerY.equalTo(bottom);
    }];
    NSMutableArray *arr_M = [NSMutableArray array];
    NSArray *titleArr = @[@"更换背景",@"人像美颜",@"一键装换"];
    NSArray *titleArr1 = @[@"mf_tabbar_1",@"mf_tabbar_3",@"mf_tabbar_2"];
    NSArray *titleArr2 = @[@"mf_tabbar_1_select",@"mf_tabbar_3_select",@"mf_tabbar_2_select"];
    for (int i = 0; i < 3; i ++) {
        
        UIView *btnBg = [[UIView alloc]init];
        btnBg.tag = i;
        [bottom addSubview:btnBg];
        
        UIButton *btn = [[UIButton alloc] init];
        btn.userInteractionEnabled = NO;
        [btn setImage:[UIImage imageNamed:titleArr1[i]] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:titleArr2[i]] forState:UIControlStateSelected];
        [btn setTitle:titleArr[i] forState:UIControlStateNormal];
        [btn setTitleColor:kUIColorFromRGA(0x000000, 0.25) forState:UIControlStateNormal];
        [btnBg addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(btnBg.mas_centerX);
            make.top.equalTo(btnBg.mas_top).offset(kRatio(5));
            make.width.height.mas_equalTo(kRatio(22));
        }];
        
        UILabel *label = [[UILabel alloc]init];
        label.text = titleArr[i];
        label.font = [UIFont systemFontOfSize:kRatio(10)];
        label.textColor = kUIColorFromRGA(0x000000, 0.25);
        [btnBg addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(btnBg.mas_centerX);
            make.bottom.equalTo(btnBg.mas_bottom).offset(kRatio(-6));
            make.height.mas_equalTo(kRatio(12));
        }];
        
        if (i == 0) {
            btn.selected = YES;
        }
        [btnBg addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gesturesToBtnClick:)]];
        
        [arr_M addObject:btnBg];
        [self.btnArr addObject:btn];
    }
    
    [arr_M mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:kRatio(20) leadSpacing:kRatio(80) tailSpacing:kRatio(80)];
    
    [arr_M mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bottom);
        make.height.mas_equalTo(kRatio(50));
    }];
    
    self.changeBg = [[ChangeBgView alloc]initWithFrame:CGRectMake(0, kRatio(13), kScreenWidth, kRatio(160)) withCount:self.info.img_wm_url_list.count];
    [footBg addSubview:self.changeBg];
    
    self.likeBeauty = [[LikeBeautyView alloc]initWithFrame:CGRectMake(0, kRatio(13), kScreenWidth, kRatio(160))];
    self.likeBeauty.hidden = YES;
    [footBg addSubview:self.likeBeauty];
    
    self.dressUp = [[DressUpView alloc]initWithFrame:CGRectMake(0, kRatio(13), kScreenWidth, kRatio(160))];
    self.dressUp.hidden = YES;
    [footBg addSubview:self.dressUp];
    
    [self.viewArr addObject:self.changeBg];
    [self.viewArr addObject:self.likeBeauty];
    [self.viewArr addObject:self.dressUp];
    
    __weak MainFunctionViewController *songself = self;
    
    self.changeBg.ChangeBgClick = ^(int count) {
        
        WCDelegate.currbgCount = count;
        
        [songself.photoImage sd_setImageWithURL:[NSURL URLWithString:songself.info.img_wm_url_list[count]]];
    };
    
    self.likeBeauty.LikeBeautyClick = ^(int value) {
        if (WCDelegate.currClothes.length > 0) {
            NSMutableDictionary *parametrs = [[NSMutableDictionary alloc]init];
            [parametrs setObject:WCDelegate.currClothes forKey:@"clothes"];
            [parametrs setObject:[NSNumber numberWithInt:WCDelegate.currFairLevel] forKey:@"fair_level"];
            [parametrs setObject:[NSNumber numberWithInt:WCDelegate.currSpecId] forKey:@"spec_id"];
            
            [ZHProgressHUD showMessage:@"正在美颜"];
            [[NetworkMgr shareInstance] uploadImage:WCDelegate.currImage withApi:@"cut_change_clothes" withParameters:parametrs success:^(NSDictionary * _Nonnull result, int code) {
                [ZHProgressHUD hideHUD];
                [MBProgressHUD hideHUD];
                if (code == 200) {
                    CutCheckPicInfo *info = [[CutCheckPicInfo alloc] initWithDic:result];
                    songself.info = info;
                    
                    [songself refreshView];
                    
                } else {
                    [ZHProgressHUD hideHUD];
                    [MBProgressHUD showError:@"美颜失败"];
                    NSString *error = [result objectForKey:@"error"];
                    NSLog(@"error = %@",error);
                }
            } failure:^(NSError * _Nonnull error) {
                [ZHProgressHUD hideHUD];
                [MBProgressHUD showError:@"美颜失败！"];
            } withAppkey:kAppkey2];
        } else {
            NSMutableDictionary *parametrs = [[NSMutableDictionary alloc]init];
            [parametrs setObject:@"1" forKey:@"is_fair"];
            [parametrs setObject:[NSNumber numberWithInt:WCDelegate.currFairLevel] forKey:@"fair_level"];
            [parametrs setObject:[NSNumber numberWithInt:WCDelegate.currSpecId] forKey:@"spec_id"];
            
            [ZHProgressHUD showMessage:@"正在美颜"];
            [[NetworkMgr shareInstance] uploadImage:WCDelegate.currImage withApi:@"cut_check_pic" withParameters:parametrs success:^(NSDictionary * _Nonnull result, int code) {
                [MBProgressHUD hideHUD];
                [ZHProgressHUD hideHUD];
                NSDictionary *dic = [result objectForKey:@"result"];
                if (code == 200) {
                    CutCheckPicInfo *info = [[CutCheckPicInfo alloc] initWithDic:dic];
                    songself.info = info;
                    
                    [songself refreshView];
                    
                } else {
                    [ZHProgressHUD hideHUD];
                    [MBProgressHUD showError:@"美颜失败！"];
                    NSString *error = [result objectForKey:@"error"];
                    NSLog(@"error = %@",error);
                }
            } failure:^(NSError * _Nonnull error) {
                [ZHProgressHUD hideHUD];
                [MBProgressHUD showError:@"美颜失败！"];
            }];
        }
    };
    
    self.dressUp.DressUpClick = ^(NSString * _Nonnull clothes) {
        if ([clothes isEqualToString:@""]) {
            NSMutableDictionary *parametrs = [[NSMutableDictionary alloc]init];
            [parametrs setObject:@"1" forKey:@"is_fair"];
            [parametrs setObject:[NSNumber numberWithInt:WCDelegate.currFairLevel] forKey:@"fair_level"];
            [parametrs setObject:[NSNumber numberWithInt:WCDelegate.currSpecId] forKey:@"spec_id"];
            
            [ZHProgressHUD showMessage:@"正在处理"];
            [[NetworkMgr shareInstance] uploadImage:WCDelegate.currImage withApi:@"cut_check_pic" withParameters:parametrs success:^(NSDictionary * _Nonnull result, int code) {
                [MBProgressHUD hideHUD];
                [ZHProgressHUD hideHUD];
                NSDictionary *dic = [result objectForKey:@"result"];
                if (code == 200) {
                    CutCheckPicInfo *info = [[CutCheckPicInfo alloc] initWithDic:dic];
                    songself.info = info;
                    
                    [songself refreshView];
                    
                } else {
                    [ZHProgressHUD hideHUD];
                    [MBProgressHUD showError:@"处理失败！"];
                    NSString *error = [result objectForKey:@"error"];
                    NSLog(@"error = %@",error);
                }
            } failure:^(NSError * _Nonnull error) {
                [ZHProgressHUD hideHUD];
                [MBProgressHUD showError:@"处理失败！"];
            }];
        } else {
            NSMutableDictionary *parametrs = [[NSMutableDictionary alloc]init];
            [parametrs setObject:clothes forKey:@"clothes"];
            [parametrs setObject:[NSNumber numberWithInt:WCDelegate.currSpecId] forKey:@"spec_id"];
            
            [ZHProgressHUD showMessage:@"正在换装"];
            [[NetworkMgr shareInstance] uploadImage:WCDelegate.currImage withApi:@"cut_change_clothes" withParameters:parametrs success:^(NSDictionary * _Nonnull result, int code) {
                [ZHProgressHUD hideHUD];
                [MBProgressHUD hideHUD];
                if (code == 200) {
                    CutCheckPicInfo *info = [[CutCheckPicInfo alloc] initWithDic:result];
                    songself.info = info;
                    
                    [songself refreshView];
                    
                } else {
                    [ZHProgressHUD hideHUD];
                    [MBProgressHUD showError:@"换装失败"];
                    NSString *error = [result objectForKey:@"error"];
                    NSLog(@"error = %@",error);
                }
            } failure:^(NSError * _Nonnull error) {
                [ZHProgressHUD hideHUD];
                [MBProgressHUD showError:@"换装失败！"];
            } withAppkey:kAppkey2];
        }
    };
}

- (void)gesturesToBtnClick:(UITapGestureRecognizer *)tap
{
    for (UIButton *btn in self.btnArr) {
        if ([btn isEqual:self.btnArr[tap.view.tag]]) {
            btn.selected = YES;
        } else {
            btn.selected = NO;
        }
    }
    switch (tap.view.tag) {
        case 0:
        {
            for (UIView *view in self.viewArr) {
                view.hidden = YES;
            }
            self.changeBg.hidden = NO;
        }
            break;
            
        case 1:
        {
            for (UIView *view in self.viewArr) {
                view.hidden = YES;
            }
            self.likeBeauty.hidden = NO;
        }
            break;
            
        case 2:
        {
            for (UIView *view in self.viewArr) {
                view.hidden = YES;
            }
            self.dressUp.hidden = NO;
        }
            break;
            
        default:
            break;
    }
}

- (void)onmfBackClick
{
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"制作未完成是否返回？"
                                                                     message:nil
                                                              preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [WCDelegate clearData];
        [self.navigationController popViewControllerAnimated:YES];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [alertVc addAction:action2];
    [alertVc addAction:action1];
    
    [[UIViewController currentUIViewController] presentViewController:alertVc
                                                             animated:YES
                                                           completion:nil];
}

- (void)onmfmForwardClick
{
    ExportViewController *vc = [ExportViewController initWtihCutCheckPicInfo:self.info];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)refreshView
{
    [self.photoImage sd_setImageWithURL:[NSURL URLWithString:self.info.img_wm_url_list[WCDelegate.currbgCount]]];
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return NO;
}
@end
